#include <iostream>
#include <cstring>
using namespace std;

/*
	观察如下代码的现象
	   1.我自己没有写拷贝构造函数，因此调用的一定是系统提供的
	     Cat(const Cat &other) //代码仅仅是简单的赋值
		 {
			 this->name=other.name;  //name现在定义的是指针，指针赋值表示两个指针指向同一个地址
			 this->age=other.age;
			 this->weight=other.weight;
		 }
	浅拷贝：如果我们使用默认的拷贝构造函数，创建出来的新对象跟原来的对象会共用同一块堆空间，从而导致原对象修改堆空间的数据，新对象也会改变
	深拷贝：程序员知道浅拷贝的bug，然后自己动手写个拷贝构造函数成功解决默认拷贝构造函数的问题--》称之为深拷贝
*/

class Cat
{
public:
    Cat(const char *name, int age, float weight)
    {
        this->name = new char[20];
        // 把实参拷贝到私有成员name中
        strcpy(this->name, name);

        this->age = age;
        this->weight = weight;
        cout << "有参构造函数" << endl;
    }
    Cat()
    {
        cout << "无参构造函数" << endl;
    }

    void show()
    {
        cout << "name:" << name << endl;
        cout << "age:" << age << endl;
        cout << "weight:" << weight << endl;
        cout << "name的地址: " << (void *)name << endl;
        cout << "age的地址: " << &age << endl;
        cout << "weight的地址: " << &weight << endl;
    }

    void setAttr(const char *name, int age, float weight)
    {
        strcpy(this->name, name);
        this->age = age;
        this->weight = weight;
    }

private:
    char *name;
    int age;
    float weight;
};

int main()
{
    // 创建猫对象
    Cat c1("旺财", 5, 15.5);

    Cat c2 = c1; // 调用拷贝构造函数
    c1.show();
    c2.show();

    c1.setAttr("小黑", 3, 10.5);
    c1.show();
    c2.show();
    return 0;
}
